<?php

namespace Apipad\Controller;
//商品规格管理
class GoodsAttrController extends CommonController
{

    //列表
    public function index(){
        $detail = I('');
        $goods_id= $detail['goods_id'];
        if(empty($goods_id)){
            $data['code'] = 0;
            $data['msg'] = '商品ID不能为空';
            exit(json_encode($data));
        }
        $page=$detail['page']?$detail['page']:1;
        $limit=$detail['limit']?$detail['limit']:20;
        $kewords=$detail['kewords'];
        $g_model = D('UserGoods');
        $ga_model = D('UserGoodsAttr');
        $g_info = $g_model->where(['id'=>$goods_id,'uid'=>UID,'isdel'=>0])->find();
        if(empty($g_info)){
            $data['code'] = 0;
            $data['msg'] = '商品信息不存在';
            exit(json_encode($data));
        }
        $map = [];
        if(I('keyword')){
            $map['color|guige_name'] = array('like','%'.I('keyword').'%');
        }
        $map['goods_id'] = $goods_id;
        $map['isdel'] = 0;
        $map['uid'] = UID;
        $count = $ga_model->where($map)->count();
        $list = $ga_model->where($map)->order('add_time desc')->page($page,$limit)->select();
        foreach($list as $k=>$v){
            $amount = bcmul($v['stock'],$v['price'],2);
            $list[$k]['amount']=$amount?:0;
        }
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['info'] = $g_info;
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }
    public function store_index(){
        $detail = I('post.');
        $goods_id = $detail['goods_id'];
        $goods_attr_id = $detail['goods_attr_id'];
        $page=$detail['page']?$detail['page']:1;
        $limit=$detail['limit']?$detail['limit']:20;
        if(empty($goods_id)){
            $data['code'] = 0;
            $data['msg'] = '商品ID不能为空';
            exit(json_encode($data));
        }
        if(empty($goods_attr_id)){
            $data['code'] = 0;
            $data['msg'] = '商品规格详情ID不能为空';
            exit(json_encode($data));
        }
        $g_model = M('user_goods');
        $ga_model = D('UserGoodsAttr');
        $g_info = $g_model->where(['id'=>$goods_id,'uid'=>UID,'isdel'=>0])->find();
        if(empty($g_info)){
            $data['code'] = 0;
            $data['msg'] = '商品信息不存在';
            exit(json_encode($data));
        }
        $ga_info = $ga_model->where(['id'=>$goods_attr_id,'uid'=>UID,'isdel'=>0])->find();
        if(empty($g_info)){
            $data['code'] = 0;
            $data['msg'] = '商品规格信息不存在';
            exit(json_encode($data));
        }
        $map['product_id'] = $goods_id;
        $map['item_id'] = $goods_attr_id;
        $ware_model = D('UserKuerpWarehouseLocProduct');
        $count = $ware_model->where($map)->count();
        $list = $ware_model->where($map)->page($page,$limit)->order('item_id desc')->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }
    //添加
    public function add(){
        $user_goods_attr=M("user_goods_attr"); 
        $detail = I('post.');
        $goods_id = $detail['goods_id'];
        if(empty($goods_id)){
            $data['code'] = 0;
            $data['msg'] = '商品id不能为空';
            exit(json_encode($data));
        }
        if(empty($detail['color'])){
            $data['code'] = 0;
            $data['msg'] = '请填写颜色';
            exit(json_encode($data));
        }
        if(empty($detail['guige_mame'])){
            $data['code'] = 0;
            $data['msg'] = '请填写规格';
            exit(json_encode($data));
        }
        if(empty($detail['price'])){
            $data['code'] = 0;
            $data['msg'] = '请填写单价';
            exit(json_encode($data));
        }
        $model = D('UserGoodsAttr');
        $g_model = D("UserGoods");
        $model->startTrans();
        $g_info = $g_model->where(['id'=>$goods_id,'uid'=>UID,'isdel'=>0])->find();
        if(empty($g_info)){
            $data['code'] = 0;
            $data['msg'] = '商品信息不存在';
            exit(json_encode($data));
        }
        $where['uid'] = UID;
        $where['goods_id'] = $goods_id;
        $where['color'] = $detail['color'];
        $where['guige_name'] = $detail['guige_mame'];
        $where['isdel'] = 0;
        $info = $model->where($where)->select();
        if($info){
            $data['code'] = 0;
            $data['msg'] = '当前规格信息已存在';
            exit(json_encode($data));
        }
        $attr_data['goods_id']=$goods_id;
        $attr_data['uid'] = UID;
        $attr_data['color']=$detail['color'];
        $attr_data['guige_name']=$detail['guige_mame'];
        $attr_data['price']=$detail['price'];
        $attr_data['dis_price']=$detail['dis_price']?:0;

        $attr_data['goods_sn']=$g_info['goods_sn'];
        $attr_data['goods_attr']=$g_info['goods_attr'];
        $attr_data['chenfen']=$g_info['chenfen'];
        $attr_data['unit']=$g_info['unit'];
        $attr_data['goods_name']=$g_info['goods_name'];
        $attr_data['add_time']=time();
        $result=$model->add($attr_data);
        if($result) {
            $model->commit();
            $data_cc['code'] = 200;
            $data_cc['msg'] = "添加成功";
            $data_cc['data'] = $result;
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "添加失败";
            exit(json_encode($data_cc));
        }
    }
    //详情
    public function detail(){
        $detail = I('');
        $goods_attr_id = $detail['goods_attr_id'];
        if(empty($goods_attr_id)){
            $data['code'] = 0;
            $data['msg'] = '商品规格详情ID不能为空';
            exit(json_encode($data));
        }
        $model = D('UserGoodsAttr');
        $map['id'] = $goods_attr_id;
        $map['uid'] = UID;
        $map['isdel'] = 0;
        $info = $model->where($map)->find();
        if($info){
            $data['code'] = 200;
            $data['msg'] = '查询成功';
            $data['data'] = $info;
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '数据不存在';
            exit(json_encode($data));
        }
    }
    //编辑
    public function edit(){
        $detail = I('post.');
        $goods_attr_id = $detail['goods_attr_id'];
        if(empty($goods_attr_id)){
            $data['code'] = 0;
            $data['msg'] = '商品规格详情ID不能为空';
            exit(json_encode($data));
        }
        if(empty($detail['color'])){
            $data['code'] = 0;
            $data['msg'] = '请填写颜色';
            exit(json_encode($data));
        }
        if(empty($detail['guige_mame'])){
            $data['code'] = 0;
            $data['msg'] = '请填写规格';
            exit(json_encode($data));
        }
        if(empty($detail['price'])){
            $data['code'] = 0;
            $data['msg'] = '请填写单价';
            exit(json_encode($data));
        }
        $model = D('UserGoodsAttr');
        $ga_info = $model->where(['id'=>$goods_attr_id,'uid'=>UID,'isdel'=>0])->find();
        if(empty($ga_info)){
            $data['code'] = 0;
            $data['msg'] = '商品规格信息不存在';
            exit(json_encode($data));
        }

        $where['uid'] = UID;
        $where['id']=array('neq',$goods_attr_id);
        $where['goods_id'] = $ga_info['goods_id'];
        $where['color'] = $detail['color'];
        $where['guige_name'] = $detail['guige_mame'];
        $where['isdel'] = 0;
        $info = $model->where($where)->select();
        if($info){
            $data['code'] = 0;
            $data['msg'] = '当前规格信息已存在';
            exit(json_encode($data));
        }
        $model->startTrans();
        $attr_data['color']=$detail['color'];
        $attr_data['guige_name']=$detail['guige_mame'];
        $attr_data['price']=$detail['price'];
        $attr_data['dis_price']=$detail['dis_price']?:0;
        $result =$model->where(['id'=>$goods_attr_id,'uid'=>UID])->save($attr_data);
        if($result) {
            $model->commit();
            $data_cc['code'] = 200;
            $data_cc['msg'] = "编辑成功";
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = "编辑失败";
            exit(json_encode($data_cc));
        }
    }
    //删除
    public function del(){
        $detail = I('');
        $goods_attr_id = $detail['goods_attr_id'];
        if(empty($goods_attr_id)){
            $data['code'] = 0;
            $data['msg'] = '商品规格详情ID不能为空';
            exit(json_encode($data));
        }
        $model = D('UserGoodsAttr');
     
        $ga_info = $model->where(['id'=>$goods_attr_id,'uid'=>UID,'isdel'=>0])->find();
        if(empty($ga_info)){
            $data['code'] = 0;
            $data['msg'] = '商品规格信息不存在';
            exit(json_encode($data));
        }
        //存在仓库数据  不进行删除
        $s_model = D('UserKuerpWarehouseLocProduct');
        $s_where['uid']=UID;
        $s_where['product_id'] = $ga_info['goods_id'];
        $s_where['item_id'] = $goods_attr_id;
        $s_info = $s_model->where($s_where)->select();
        if($s_info){
            $data['code'] = 0;
            $data['msg'] = '存在仓库数据,不进行删除';
            exit(json_encode($data));
        }
        $map['id'] = $goods_attr_id;
        $map['uid'] = UID;
        $s_data['isdel'] = 1;
        $s_data['del_time'] = time();
        $model->startTrans();
        $result = $model->where($map)->save($s_data);
        if($result){
            $model->commit();
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }
    
}
